﻿

using Furion;
using IotContract.Dtos.Remotes.Request;
using IotContract.Dtos.Servers.Process;
using IotContract.Dtos.Servers.Pushs;
using IotContract.Grpcs;
using IotContract.Models;
using IotContract.Models.Clients;
using IotContract.Models.Mqtt;
using IotContract.Models.Process;
using IotContract.Models.Webs;
using IotFun.Models;
using IotFun.Option;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using XT.Grpc.Services;

namespace IotFun.Services;

public class GrpcService
{
    private readonly GrpcChannelService _grpc;
    private IConnectGrpcService _connectGrpcService;
    public GrpcService(GrpcChannelService grpcChannelService) { _grpc = grpcChannelService; }

    /// <summary>
    /// 创建Grpc连接服务
    /// </summary>
    /// <returns></returns>
    private async Task CreateConnectGrpcService()
    {
        if (_connectGrpcService == null || !_grpc.IsConnect)
        {
            var option = App.GetOptions<IotFunOptions>();
            _connectGrpcService = await _grpc.CreateGrpcService<IConnectGrpcService>(option.GrpcUrl);
        }
    }

    /// <summary>
    /// 获取统计信息
    /// </summary>
    /// <returns></returns>
    public async Task<GrpcReturn<List<GroupRunningData>>> GetGroupStatistic()
    {

        return new GrpcReturn<List<GroupRunningData>>
          {
             Data= new List<GroupRunningData>
              {
                  new GroupRunningData
                  {
                      GroupName="物流线",
                      StopNum=1,
                      TotalNum=20
                  },
                  new GroupRunningData
                  {
                      GroupName="堆垛机",
                      StopNum=2,
                      TotalNum=10
                  },
                  new GroupRunningData
                  {
                      GroupName="OCV",
                      StopNum=0,
                      TotalNum=15
                  },
                   new GroupRunningData
                  {
                      GroupName="充放电",
                      StopNum=1,
                      TotalNum=20
                  },
                  new GroupRunningData
                  {
                      GroupName="筛选机",
                      StopNum=2,
                      TotalNum=10
                  },
                  new GroupRunningData
                  {
                      GroupName="扫码枪",
                      StopNum=0,
                      TotalNum=15
                  }
              },
             IsSuccess=true
          };
       
    }

    /// <summary>
    /// 发送控制命令
    /// </summary>
    /// <param name="commands"></param>
    /// <returns></returns>
    public async Task<string> ControlCommand(List<ClientControlCommand> commands)
    {
        await CreateConnectGrpcService();
        var result = await _connectGrpcService.ControlCommands(commands);

        
        return result.Msg;
    }

    public async Task<string> CompleteJob(FmsJobCompleteRequest fmsJobCompleteRequest)
    {
        await CreateConnectGrpcService();
       var result= await _connectGrpcService.CompleteJob(fmsJobCompleteRequest);

        return result.Msg;
    }
    /// <summary>
    /// 获取设备数据
    /// </summary>
    /// <param name="commands"></param>
    /// <returns></returns>
    public async Task<List<PushEquipWeb>> GetEquips(List<ClientRequestCommand> commands)
    {
        await CreateConnectGrpcService();
        var result = await _connectGrpcService.RequestPushEquips(commands);
        return result.Data;
    }

    /// <summary>
    /// 获取点位数据
    /// </summary>
    /// <param name="commands"></param>
    /// <returns></returns>
    public async Task<List<PushTagWeb>> GetEquipTags(ClientRequestCommand command)
    {
        try
        {
            await CreateConnectGrpcService();
            var result = await _connectGrpcService.RequestPushTags(command);
            return result.Data;
        }
        catch(Exception ex)
        {
            return new List<PushTagWeb>();
        }
    }

    /// <summary>
    /// 写入设备数据
    /// </summary>
    /// <param name="commands"></param>
    /// <returns></returns>
    public async Task<bool> WriteEquips(List<OperationWriteCommand> commands)
    {
        await CreateConnectGrpcService();
        var result = await _connectGrpcService.AddWriteCommand(commands);
        return result.IsSuccess;
    }
    /// <summary>
    /// 获取调度数据
    /// </summary>
    /// <param name="command"></param>
    /// <returns></returns>
    public async Task<List<ProcessBaseData>> GetProcessDatas(ProcessProviderCommand command)
    {
        await CreateConnectGrpcService();
        var result = await _connectGrpcService.RequestProcessDatas(command);
        return result.Data;
    }
    /// <summary>
    /// 移除任务数据
    /// </summary>
    /// <param name="command"></param>
    /// <returns></returns>
    public async Task<string> RemoveProcessJob(ProcessJobCommand command)
    {
        await CreateConnectGrpcService();
        var result = await _connectGrpcService.RemoveProcessJob(command);
        return result.Data;
    }

    /// <summary>
    /// 添加任务数据
    /// </summary>
    /// <param name="command"></param>
    /// <returns></returns>
    public async Task<string> AddProcessJob(ProcessJobCommand command)
    {
        await CreateConnectGrpcService();
        var result = await _connectGrpcService.AddProcessJob(command);
        return result.Data;
    }

    /// <summary>
    /// 执行调度数据
    /// </summary>
    /// <param name="command"></param>
    /// <returns></returns>
    public async Task<string> ExecuteProcessData(ProcessJobCommand command)
    {
        await CreateConnectGrpcService();
        var result = await _connectGrpcService.ExecuteProcessData(command);
        return result.Data;
    }


    /// <summary>
    /// 通过GRPC获取mqtt的客户端数据
    /// </summary>
    /// <param name="command"></param>
    /// <returns></returns>
    public async Task<List<MqttClientInfo>> GetMqttClientInfos(MqttProviderCommand command)
    {
        await CreateConnectGrpcService();
        var result = await _connectGrpcService.RequestMqttClientInfos(command);
        return result.Data;
    }


    public async Task<MqttGrpcReturn> StartOrStopMqttServer(MqttProviderCommand command)
    {
        await CreateConnectGrpcService();
        var result = await _connectGrpcService.StartOrStopMqttServer(command);
        return result.Data;
    }
    public async Task<MqttGrpcReturn> GetMqttServerStatus()
    {
        await CreateConnectGrpcService();
        var result = await _connectGrpcService.RequestMqttServerStatus();
        return result.Data;
    }
    public async Task<MqttGrpcReturn> StartOrStopMqttProcessData(MqttProviderCommand command)
    {
        await CreateConnectGrpcService();
        var result = await _connectGrpcService.StartOrStopMqttProcessData(command);
        return result.Data;
    }
    
    public async Task<MqttGrpcReturn> GetDataPublishStatus()
    {
        await CreateConnectGrpcService();
        var result = await _connectGrpcService.GetDataPublishStatus();
        return result.Data;
    }


}
